package com.pan.download2;

import com.pan.download2.entiry.ChapterInfo;
import org.openqa.selenium.By;
import org.openqa.selenium.WebElement;
import org.openqa.selenium.edge.EdgeDriver;

import java.util.List;
import java.util.concurrent.Callable;
import java.util.stream.Collectors;

/**
 * 获取小说目录 只能接收这个网站的
 * www.kbwang.org
 */
public class Directory {

    private EdgeDriver edgeDriver;

    private String directoryUrl;

    public Directory(String directoryUrl) {
        this.directoryUrl = directoryUrl;
    }

    public List<ChapterInfo> get() {
        this.edgeDriver = new EdgeDriver();
        edgeDriver.navigate().to(directoryUrl);
        //确保页面加载到底部
        edgeDriver.findElement(By.id("footer"));
        //所有章节目录
        List<WebElement> aList =  edgeDriver.findElement(By.id("list"))
                .findElements(By.cssSelector("a"));
        //跳过前9章
        List<ChapterInfo> chapterInfoList = aList.stream().skip(9).map(a->{
            String url =  a.getAttribute("href");
            String title = a.getText();
            ChapterInfo chapterInfo = new ChapterInfo(url,title);
            return chapterInfo;
        }).collect(Collectors.toList());
        edgeDriver.quit();
        return chapterInfoList;
    }
}
